<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue</title>
  <script src="../js/vue.js"></script>
</head>
<body>
  <div id="app">
    <button @click="currentCom = 'component1'">按钮1</button>
    <button @click="currentCom = 'component2'">按钮2</button>
    <button @click="currentCom = 'component3'">按钮3</button>
    <!-- 动态组件 -->
    <!-- 通过keep-alive标签将动态组件进行缓存处理 -->
    <keep-alive>
      <component :is="currentCom"></component>
    </keep-alive>
  </div>
  <script>
    let component1 = {
      template: `
        <div>组件一内容</div>
      `,
      created(){
        console.log('组件一created');
      }
    }
    let component2 = {
      template: `
        <div>组件二内容</div>
      `,
      created(){
        console.log('组件二created');
      }
    }
    let component3 = {
      template: `
        <div>组件三内容</div>
      `,
      created(){
        console.log('组件三created');
      }
    }
    let vm = new Vue({
      el: '#app',
      data: {
        currentCom: 'component1'
      },
      components: {
        'component1': component1,
        'component2': component2,
        'component3': component3,
      }
    })
  </script>
</body>
</html>